package com.shuqi.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.shuqi.activity.viewport.BookContentMenuView;
import com.shuqi.common.PayTheme;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.SharedPreferenceUtil;
import com.shuqi.controller.R;
import com.shuqi.model.BookContentSettings;

/**
 * 收费版阅读页 主题listview 适配器
 * 
 * @author vip
 */
public class PayBcMenuThemAdapter extends BaseAdapter {
    /** 加载layout */
    private LayoutInflater inflater;
    /** 设置功能对象 */
    private BookContentSettings settings;
    /** 列表显示的icon */
    private int[] drawables = {R.drawable.icon_them0, R.drawable.icon_them1, R.drawable.icon_them2,
            R.drawable.icon_them3, R.drawable.icon_them4, R.drawable.icon_them5,
            R.drawable.icon_them6, R.drawable.icon_them7};
    /** 区分是否是夜间模式 */
    private boolean isNightModel;
    /** 夜间列表显示的icon */
    private int[] nightDrawables = {R.drawable.icon_them_night0, R.drawable.icon_them_night1,
            R.drawable.icon_them_night2, R.drawable.icon_them_night3, R.drawable.icon_them_night4,
            R.drawable.icon_them_night5, R.drawable.icon_them_night6, R.drawable.icon_them_night7};

    /**
     * 阅读页 主题listview 适配器
     * 
     * @param context
     * @param settings
     */
    public PayBcMenuThemAdapter(Context context, BookContentSettings settings, BookContentMenuView menu) {
        this.inflater = LayoutInflater.from(context);
        this.settings = settings;
        this.menuView = menu;
    }
    BookContentMenuView menuView;

    /**
     * 设置是否是夜间模式
     */
    public void setNightMode(boolean isNight) {
        this.isNightModel = isNight;
    }

    @Override
    public int getCount() {
        return drawables.length;
    }

    @Override
    public Object getItem(int position) {
        return drawables[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(position == 0){
            menuView.onTop();
        }else if(position ==7){
            menuView.onBottom();
        }
        
        
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.p_item_bcmenu_them, parent, false);
            RankListHolder holder = new RankListHolder();
            holder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_them_icon);
            holder.iv_stroke = (ImageView) convertView.findViewById(R.id.iv_them_stroke);
            convertView.setTag(holder);
        }

        try {
            RankListHolder holder = (RankListHolder) convertView.getTag();
            if (this.isNightModel) {
                holder.iv_icon.setBackgroundResource(nightDrawables[position]);
                holder.iv_stroke.setBackgroundResource(R.drawable.p_bg_menu_theme_box_night);
            } else {
                holder.iv_icon.setBackgroundResource(drawables[position]);
                holder.iv_stroke.setBackgroundResource(R.drawable.p_bg_menu_theme_box);
            }
            if (position == settings.getTheme()) {
                holder.iv_stroke.setVisibility(View.VISIBLE);
            } else {
                holder.iv_stroke.setVisibility(View.GONE);
            }
            //夜间选中上一个
            int lastTheme =
                    SharedPreferenceUtil.getInstanceSharedPreferenceUtil()
                    .getBookContentLastTheme();
            if(settings.getTheme() == PayTheme.PAY_THEME && position == lastTheme){
                holder.iv_stroke.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }

        return convertView;
    }

    private static class RankListHolder {
        public ImageView iv_icon;
        public ImageView iv_stroke;
    }

}
